package com.ywh.auth;

import org.apache.commons.codec.binary.Base64;

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;

public class JwtToken {

    private static Base64 base64 = new Base64();

    public static void main(String[] args) throws Exception {
    /*    String a = "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJIaFNzVTBOa0Y2UjNQa01ZcVB6ajJxcHpvVWRyZVpWUUo2YmlQd01LTTBZIn0.eyJqdGkiOiJhZjY1ZmJiNC1kYWRmLTRiNDUtODMyZC1lMmY1NDNmMWU2ZjMiLCJleHAiOjE1NjU5NDQ3MzIsIm5iZiI6MCwiaWF0IjoxNTY1OTQ0NDMyLCJpc3MiOiJodHRwOi8vMTI3LjAuMC4xOjgwODAvYXV0aC9yZWFsbXMvc3ByaW5nLWFwaSIsImF1ZCI6InNwcmluZy1jbGllbnQiLCJzdWIiOiIzMjgxNzBkMy04OGI1LTQ0MGYtODU1Yy0wMGNkOTE2YzgzZmIiLCJ0eXAiOiJJRCIsImF6cCI6InNwcmluZy1jbGllbnQiLCJhdXRoX3RpbWUiOjE1NjU5NDQ0MzIsInNlc3Npb25fc3RhdGUiOiI4MTZiMzNkOS1mYTVhLTQyNmYtYmZkNi00Nzk4NGYzZTYwMzYiLCJhY3IiOiIxIiwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJ1c2VyIiwiZW1haWwiOiJ5a2FzYmRnQHFxLmNvbSJ9";
        String pkey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqMvvUYyO3naTvlY6dYggLD1SNFepSsr3/MN4RACtGi3HNXV1E/gWGXAufwd9pwzOFHTWJ7m4IHzTe2p1u27fPGmQBoIFL7nrtI1G2XY7vXTMImZDepp4+nB6rDt3L/QhTrAdx73XWWMSQb8HuN1W19xBGokX9E4YUSJiq7QVJmJ2W2HeX3cBdoYcvysIXo0vTrHU6k25blPFaLFuXrR3NGKrEKLJ+v/VNIjHoNVnMO6Y8apPEZ07XafMwkXuqSOkFx0QcJRvLPv3YTdwfRsGpE9KJIt9HWd83Pl/IdvdTNsQDSKHNcz2Nqp5jr5HQJ2Wqcat1msNRlLoNCko5sL1cQIDAQAB";
        String sign = "Z4cqQ5qwq8UAW3Dapwz8ORBQNiU6nzBmYOMV5XygLyhWHuGc2zWfF5ls5H0jTlgOQaeNqAto5tr_Ta8OOYn3vIqt8n2METOWwj_wqo9_vNekdVzp4aHAR7OMbuZ21FFeCNO3KHk1rO0zEegk_i-c1NlxAAVFjZzuuav6G_6wosfyjcG7SOAek5TSkaMVppjysTMT0TqE4u8HWUA_wDvuEXzafyxWmvSevf-Pys0f1hYocGtkq9Gdi6PVMKYqOt1Q9HwIy1K_ZsIy86S_Jsu-yh2XvzDSfbhUskBsnA0vUDxjfxPeyv9jXpBDF3bWzrBkQ0Keii-H3dT5K1WPFiVeCA";
        boolean b = checkSign(a, pkey, sign);
        System.out.println(b);*/

        byte[] b = base64.decode("eyJqdGkiOiJjMWI1ZGNmZC01YjVjLTQzZmUtYTJmMi03MjBjODUwNjU1OTIiLCJleHAiOjE1NjYxNzg3ODcsIm5iZiI6MCwiaWF0IjoxNTY2MTc4NDg3LCJpc3MiOiJodHRwOi8vMTI3LjAuMC4xOjgwODAvYXV0aC9yZWFsbXMvc3ByaW5nLWJvb3QtcXVpY2tzdGFydCIsInN1YiI6ImM1NDVhMzU0LWVlNWMtNDI4YS1iZGUwLWMyNzUyN2ZjZjA0NSIsInR5cCI6IkJlYXJlciIsImF6cCI6ImFjY291bnQiLCJhdXRoX3RpbWUiOjAsInNlc3Npb25fc3RhdGUiOiI5YTRjM2M3Zi01YzAxLTRkYjAtYWM2ZC03ZDQ5MTA4YzNmMDkiLCJhY3IiOiIxIiwicmVzb3VyY2VfYWNjZXNzIjp7ImFjY291bnQiOnsicm9sZXMiOlsibWFuYWdlLWFjY291bnQiLCJtYW5hZ2UtYWNjb3VudC1saW5rcyIsInZpZXctcHJvZmlsZSJdfX0sInNjb3BlIjoiZW1haWwgcHJvZmlsZSIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwiY2xpZW50SWQiOiJhY2NvdW50IiwiY2xpZW50SG9zdCI6IjEyNy4wLjAuMSIsInByZWZlcnJlZF91c2VybmFtZSI6InNlcnZpY2UtYWNjb3VudC1hY2NvdW50IiwiY2xpZW50QWRkcmVzcyI6IjEyNy4wLjAuMSIsImVtYWlsIjoic2VydmljZS1hY2NvdW50LWFjY291bnRAcGxhY2Vob2xkZXIub3JnIn0");
        String json = new String(b);
        System.out.println(json);
    }

    public static boolean checkSign(String preSignStr, String publicKey, String sign) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        byte[] encodedKey = base64.decode(publicKey);
        PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
        Signature signature = Signature.getInstance("SHA256WithRSA");
        signature.initVerify(pubKey);
        signature.update(preSignStr.getBytes("UTF-8"));
        boolean bverify = signature.verify(base64.decode(sign.getBytes("UTF-8")));
        return bverify;
    }
}
